package legumes.client.ui.admin.stall;

import legumes.client.services.callbacks.SetMessageStoreCallback;
import legumes.client.services.util.rpc.RPCCallSequencer;

import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.RichTextEditor;
import com.smartgwt.client.widgets.events.FocusChangedEvent;
import com.smartgwt.client.widgets.events.FocusChangedHandler;
import com.smartgwt.client.widgets.layout.HLayout;

public class NewsPanel extends HLayout {
	private RichTextEditor richTextEditor;
	
	public NewsPanel() {
		super();
		setWidth100();
		setHeight(155);
		setTitle("News");
		richTextEditor = new RichTextEditor();
		richTextEditor.setWidth100(); 
        richTextEditor.setOverflow(Overflow.SCROLL);  
        richTextEditor.setCanDragResize(true);  
        richTextEditor.setShowEdges(true);  
        richTextEditor.addFocusChangedHandler(new FocusChangedHandler() {
			
			@Override
			public void onFocusChanged(FocusChangedEvent event) {
				if( ! event.getHasFocus() ){
					saveOnServer();
				}
			}
		});
        richTextEditor.setValue("<i>Loading news...</i>");  
    	setMembers(richTextEditor);
	}
	
	public void initData(String msg_){
		if( msg_ != null && msg_.trim().length()>0){
			richTextEditor.setValue(msg_);
		} else {
			richTextEditor.setValue("<i>Aucune nouvelle n'a &eacute;t&eacute; saisie.</i>");  
		}
	}
	
	private void saveOnServer(){
		RPCCallSequencer callSequencer = new RPCCallSequencer(
		"Enregistrement en cours...");
		
		final SetMessageStoreCallback setMessageStoreCallback = new SetMessageStoreCallback(
				richTextEditor.getValue());
		callSequencer.addCall(setMessageStoreCallback);
		callSequencer.addRunnable(new Runnable() {
			public void run() {
			}
		});
		
		callSequencer.callSequence();
	}
	
}
